from django.shortcuts import render
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import viewsets
from rest_framework.filters import SearchFilter
from rest_framework.pagination import PageNumberPagination

from students.filter import StudentFilter
from students.models import City, Student
from students.serializers import CitySerializer, StudentSerializer


# Create your views here.


class Page(PageNumberPagination):
    page_size = 2
    page_query_param = 'page'
    page_size_query_param = 'size'


class CityView(viewsets.ModelViewSet):
    queryset = City.objects.all()
    serializer_class = CitySerializer

class StudentView(viewsets.ModelViewSet):
    queryset = Student.objects.all()
    serializer_class = StudentSerializer
    pagination_class = Page
    filterset_class = StudentFilter
    filter_backends = [DjangoFilterBackend,SearchFilter]
    search_fields = ['name', 'phone', 'city']